Imports System.Data
Partial Class Client_frmViewProduct
    Inherits System.Web.UI.Page
    Dim _bo As New Bo
    Dim _SchemeVo As New SchemeVo
    Dim _ProductVo As New ProductVo
    Dim _AddToCartVo As New AddToCartVo
    Dim _ProductId As String
    Dim _UserId As String
    Dim _SchemeId As String
    Dim _AvailableQuantity As String
    Dim _TotalAmount As Integer
    Dim _Count As Integer
    Dim dt As New DataTable
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _UserId = Session("UserId")
        _ProductId = Request.QueryString("ProductId")

        'get Value Of count and update it 
        dt = _bo.GetCount(_ProductId)
        _Count = Integer.Parse(dt.Rows(0).Item(0).ToString)
        _Count = _Count + 1
        _bo.UpdateCount(_Count, _ProductId)
        'Count Updated

        _ProductVo = _bo.getProductById(_ProductId)
        img.Src = _ProductVo.PhotoPath
        img.Alt = _ProductVo.PhotoPath
        'img.ImageUrl = _ProductVo.PhotoPath
        lblPrice.Text = _ProductVo.Price
        lblBrand.Text = _ProductVo.Brand
        _AvailableQuantity = Integer.Parse(_ProductVo.AvailableQuantity)
        _SchemeId = _ProductVo.SchemeId
        _SchemeVo = _bo.getSchemeById(_SchemeId)
        lblDiscount.Text = _SchemeVo.Discount + "%"
    End Sub
    Sub calculate(ByVal _Discount As String, ByVal _OriginalPrice As String, ByVal _Quantity As String)
        Dim _TotalAmount As Integer
        _AddToCartVo.ProductId = _ProductId
        _AddToCartVo.Quantity = _Quantity
        _AddToCartVo.UserId = _UserId
        _AddToCartVo.Size = ddl1.Text
        _AddToCartVo.OriginalPrice = Integer.Parse(lblPrice.Text) * Integer.Parse(_Quantity)
        Dim dis As Integer = Integer.Parse(_Discount)
        Dim Originalprice As Integer = Integer.Parse(_OriginalPrice)
        _TotalAmount = (Originalprice - ((dis * Originalprice) / 100)) * Integer.Parse(_Quantity)
        _AddToCartVo.DiscountedPrice = _TotalAmount

    End Sub

   
    Protected Sub lnkbtn_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles lnkbtn.Click
        If _UserId = "" Then
            Response.Redirect("frmLogin.aspx")
        Else
            If txtQuantity.Text = "" Then
                txtQuantity.Text = "1"
            End If
            Dim dt As New DataTable
            dt = _bo.CheckProductInCart(_ProductId, _UserId)
            If dt.Rows.Count = 0 Then
                If _AvailableQuantity < Integer.Parse(txtQuantity.Text) Then
                    MsgBox("Quantity Not In Stock ")
                Else
                    Call calculate(_SchemeVo.Discount, _ProductVo.Price, txtQuantity.Text)
                    _bo.AddToCart(_AddToCartVo)
                    Response.Redirect("frmShowCart.aspx")
                End If
            Else
                Dim ExistingQty As Integer
                Dim TotalQuantity As Integer
                Dim dtqty As New DataTable
                dtqty = _bo.getQuantity(_ProductId, _UserId)
                ExistingQty = Integer.Parse(dtqty.Rows(0).Item(0).ToString)
                TotalQuantity = (Integer.Parse(txtQuantity.Text) + ExistingQty)
                If _AvailableQuantity < TotalQuantity Then
                    MsgBox("Quantity Not In Stock ")
                Else
                    Call calculate(_SchemeVo.Discount, _ProductVo.Price, TotalQuantity)
                    _bo.UpdateToCart(_AddToCartVo)
                    Response.Redirect("frmShowCart.aspx")
                End If
            End If
        End If
    End Sub

    
End Class
